package client.ui.view.dialogview;

import client.Main;
import client.UserState;
import client.assets.Icons;
import client.control.Controller;
import client.control.ResponseEntity;
import client.ui.component.button.MaterialButton;
import client.ui.component.edittext.PasswordEdit;
import client.ui.view.base.FrameBase;
import client.ui.view.base.PanelControl;
import util.MD5Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ChangePasswordDialog {
    /**
      * @Description 显示修改密码对话框
      * @param owner 所有
      * @param parentContainer 父容器
      * @author 孔明浩
      * @Date 2020/4/13
      */
    public static void showRegisterDialog(Frame owner, Container parentContainer){
        JDialog dialog = new JDialog(owner,true);
        dialog.setSize(400,240);
        dialog.setResizable(false);
        dialog.setLocationRelativeTo(parentContainer);
        dialog.setTitle("注册账户");
        dialog.setIconImage(Icons.register.getImage());

        JButton confirm = MaterialButton.BlueButton("提交",null);
        PasswordEdit oldPasswordEdit = new PasswordEdit("请输入原密码");
        PasswordEdit passwordEdit = new PasswordEdit("请输入新密码");
        PasswordEdit confirmPassword = new PasswordEdit("请确认新密码");

        confirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (oldPasswordEdit.getText().length() == 0){
                    oldPasswordEdit.setErrorText("原密码不能为空");
                    passwordEdit.setErrorText("");
                    confirmPassword.setErrorText("");
                } else if (passwordEdit.getText().length() == 0){
                    passwordEdit.setErrorText("密码不能为空");
                    oldPasswordEdit.setErrorText("");
                    confirmPassword.setErrorText("");
                } else if (!confirmPassword.getText().equals(passwordEdit.getText())){
                    confirmPassword.setErrorText("两次输入密码不一致");
                    oldPasswordEdit.setErrorText("");
                    passwordEdit.setErrorText("");
                } else {
                    //启动新线程查询数据
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            ResponseEntity responseEntity = Controller.USER_CONTROL.modifyPassword(UserState.getToken(),
                                    MD5Util.encodeByMd5(oldPasswordEdit.getText()),
                                    MD5Util.encodeByMd5(passwordEdit.getText())
                            );
                            if (responseEntity.getCode() == 0){
                                //关闭对话框
                                SwingUtilities.invokeLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        dialog.dispose();
                                        JOptionPane.showMessageDialog(owner,"修改成功, 请重新登陆");
                                        UserState.logout();
                                        //重新启动面板
                                        FrameBase.frameBaseList.get(0).dispose();
                                        FrameBase.frameBaseList.clear();
                                        PanelControl.panels.clear();
                                        Main.main(null);
                                        dialog.dispose();
                                    }
                                });
                            } else {
                                JOptionPane.showMessageDialog(owner,"修改失败, 请检查原密码是否正确");
                            }
                        }
                    }).start();
                }
            }
        });

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        Box vBox = Box.createVerticalBox();
        vBox.add(oldPasswordEdit);
        vBox.add(passwordEdit);
        vBox.add(confirmPassword);
        vBox.add(new JLabel(" "));
        panel.add(vBox);
        panel.add(confirm);
        dialog.setContentPane(panel);

        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setUndecorated(false);
        dialog.setVisible(true);
    }
}
